package sz.com.postoffice.jxyzwgcase.service.impl;

import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import sz.com.postoffice.jxyzwgcase.core.TableDataInfo;
import sz.com.postoffice.jxyzwgcase.dao.BaseDao;
import sz.com.postoffice.jxyzwgcase.dao.master.EnterpriseCustomerDao;
import sz.com.postoffice.jxyzwgcase.pojo.EnterpriseCustomer;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceCustomerRelation;
import sz.com.postoffice.jxyzwgcase.pojo.ResourceMarket;
import sz.com.postoffice.jxyzwgcase.service.EnterpriseCustomerService;
import sz.com.postoffice.jxyzwgcase.service.ResourceMarketService;

import javax.annotation.Resource;


/**
 * @ClassName EnterpriseCustomerServiceImpl
 * @Deacription 商户逻辑处理层
 * @Author jmx
 * @Date 2020/11/20 18:00
 * @Version 1.0
 **/
@Service
@Slf4j
public class EnterpriseCustomerServiceImpl extends BaseServiceImpl<EnterpriseCustomer> implements EnterpriseCustomerService {
    private static final Logger logger= LoggerFactory.getLogger(EnterpriseCustomerServiceImpl.class);
    @Resource
    private EnterpriseCustomerDao enterpriseCustomerDao;
    @Override
    protected BaseDao<EnterpriseCustomer> getMapper() {
        return this.enterpriseCustomerDao;
    }

    @Resource
    private ResourceMarketService resourceMarketService;

    @Override
    public EnterpriseCustomer findByPrimaryKey(Integer id) {
        EnterpriseCustomer customer = enterpriseCustomerDao.findByPrimaryKey(id);
        if (customer != null) {
            ResourceCustomerRelation resourceCustomerRelation = enterpriseCustomerDao.findRelationByCustomerCode(customer.getCode());
            if (resourceCustomerRelation != null) {
                customer.setRelationId(resourceCustomerRelation.getId());
                customer.setResourceType(resourceCustomerRelation.getType());
                customer.setResourceCode(resourceCustomerRelation.getResourceCode());
                //获取资源的段道
                ResourceMarket param = new ResourceMarket();
                param.setResourceType(resourceCustomerRelation.getType());
                param.setCode(resourceCustomerRelation.getResourceCode());
                ResourceMarket resourceMarket = resourceMarketService.findByTypeAndCode(param);
                if (resourceMarket != null) {
                    customer.setResourceGrid(resourceMarket.getGridCode());
                }
            }

        }
        return customer;
    }

    @Override
    public boolean updateRelation(ResourceCustomerRelation relation) {
        boolean flag = true;
        try {
            if (relation.getId() != null && relation.getId() > 0) {
                enterpriseCustomerDao.updateRelation(relation);
            } else {
                enterpriseCustomerDao.insertRelation(relation);
            }
        } catch (Exception e) {
            flag = false;
            logger.error("更新商企资源关系失败!原因是:",e);
        }
        return flag;

    }

    @Override
    public ResourceCustomerRelation getRelationByResource(String resourceCode) {
        return enterpriseCustomerDao.findRelationByResourceCode(resourceCode);
    }
}
